import React, { Component } from 'react'
import { BrowserRouter as Router, withRouter } from 'react-router-dom';

class _GuardHelper extends Component {

    componentDidMount() {
        // 添加阻塞
        this.props.history.block('');
        
    }

    render() {
        return null;
    }
}

const GuardHelper = withRouter(_GuardHelper);

class RouteGuard extends Component {

    handleConfirm = (msg, callback) => {
        if (this.props.onBeforeChange) {
            this.props.onBeforeChange(callback);
        }
    }

    render() {
        return <Router getUserConfirmation={this.handleConfirm}>
            <GuardHelper />
            {this.props.children}
        </Router>;
    }
}

export default RouteGuard;
